//
//  SEGInviteViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/2/23.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGInviteViewController.h"
#import "SEGInviteView.h"
#import "SEGQRCodeInviteView.h"
#import "SEGInviteRecordViewController.h"

#import "SEGInviteRulesViewController.h"
#import "SEGInviteViewModel.h"

@interface SEGInviteViewController () <SEGInviteViewDelegate>
@property (nonatomic,strong) SEGInviteView *bgView;
@property (nonatomic,strong) SEGQRCodeInviteView *qrCodeView;

/**
 分享渠道
 */
@property (nonatomic, copy) NSString *channel;

@property(nonatomic, strong) SEGInviteViewModel *viewModel;
@end

@implementation SEGInviteViewController 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.title.length > 0?self.title:@"邀请邻居";
    [self requestData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareSucceed) name:NOTIFICATION_SHARESUCCEED object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_SHARESUCCEED object:nil];
}

- (void)initView
{
    self.bgView = [[SEGInviteView alloc] init];
    self.bgView.backgroundColor = [UIColor colorWithRed:254/255.0 green:231/255.0 blue:187/255.0 alpha:1];
    self.bgView.viewDelegate = self;
    self.bgView.model = self.viewModel.inviteModel;
    [self.view addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
}

- (void)requestData
{
    [SVProgressHUD show];
    [self.viewModel requestShareEntryInfo:^(NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
         [SVProgressHUD dismiss];
        if ([codeType isEqualToString:@"0"]) {
            [self initView];
        }else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
}

// 分享成功提交分享记录
- (void)shareSucceed
{
    UIViewController *vc = self.navigationController.visibleViewController;
    if ([vc isEqual:self]) {//当前显示的控制器
        [self.viewModel requestSubmitShareQcodeWithShareCode:self.viewModel.inviteModel.shareCode paperId:self.viewModel.inviteModel.paperId channel:self.channel];
    }
}

#pragma mark - SEGShareVCDelegate
- (void)shareVCActionButtonPressedWithChannel:(NSString *)channel
{
    self.channel = channel;
}


#pragma mark - SEGInviteViewDelegate
/**
 *  显示邀请码
 */
- (void)showQRCodeView
{
    if (!self.qrCodeView) {
        self.qrCodeView = [[SEGQRCodeInviteView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight())];
        [self.qrCodeView createQRCodeWithUrl:self.viewModel.inviteModel.inviteurl];
        [[[UIApplication sharedApplication] keyWindow] addSubview:self.qrCodeView];
    }
    self.qrCodeView.hidden = NO;
}

/**
 *  显示url邀请
 */
- (void)showUrlInviteView
{
    NSMutableDictionary *tmp = [NSMutableDictionary dictionary];
    [tmp setValue:self.viewModel.inviteModel.shareicon forKey:@"shareImageUrlString"];
    [tmp setValue:@(SEGShareVCType_Invite) forKey:@"shareVCType"];
    
    [tmp setValue:self.viewModel.inviteModel.sharetitle forKey:@"sharetitle"];
    [tmp setValue:self.viewModel.inviteModel.sharecontent forKey:@"sharecontent"];
    [tmp setValue:self.viewModel.inviteModel.inviteurl forKey:@"inviteurl"];
    
    WeakSelf;
    UIViewController *shareVC = [[SEGUhomeMediator sharedInstance] seg_initShareViewController:tmp actionType:^(NSInteger actionType) {
        
    } channel:^(NSString *channel) {
        weakSelf.channel = channel;
    }];
    [self presentViewController:shareVC animated:NO completion:nil];
}

/**
 *  显示邀请记录
 */
- (void)showInviteRecord
{
    SEGInviteRecordViewController *recordViewController = [[SEGInviteRecordViewController alloc] init];
    [self.navigationController pushViewController:recordViewController animated:YES];
}

/**
 *  显示邀请规则
 */
- (void)showInviteRules
{
    SEGInviteRulesViewController *recordViewController = [[SEGInviteRulesViewController alloc] init];
    recordViewController.rules = [NSMutableArray arrayWithArray:self.viewModel.inviteModel.rules];
    [self.navigationController pushViewController:recordViewController animated:YES];
}


#pragma mark - init Views
-(void)segInitViews{

}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGInviteViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGInviteViewModel alloc]init];
    }
    return _viewModel;
}
@end
